package com.itkm.converter;

import com.itkm.entity.AuthorityEnum;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

import javax.persistence.AttributeConverter;
import java.util.Arrays;
import java.util.Objects;
@Slf4j
public class AuthorityConverter implements AttributeConverter<AuthorityEnum, Integer> {
    @Override
    public Integer convertToDatabaseColumn(@NonNull AuthorityEnum authorityEnum) {
        return authorityEnum.getId();
    }

    @Override
    public AuthorityEnum convertToEntityAttribute(@NonNull Integer id) {
        return Arrays.stream(AuthorityEnum.values())
                .filter(e -> Objects.equals(e.getId(), id))
                .findFirst()
                .orElse(AuthorityEnum.UNKNOWN);
    }
}
